using System;
using System.Collections;
using System.Collections.Generic;
using PEMath;
using UnityEngine;
using Random = UnityEngine.Random;

/****************************************************
// 功能：主要表现控制 攻速/移速/技能动画播放/血条的信息显示/小地图显示ICON
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/4/1 10:44:24
*****************************************************/

public abstract class MainViewUnit : ViewUnit
{
    public Transform skillRange;
    public float fade;
    // 血条定位点
    public Transform hpRoot;
    public Animation ani;
    
    public HpWindow hpWindow;
    private PlayerControlWindow playerWindow;

    private float aniMoveSpeedBase;
    private float aniAttackSpeedBase;
    private MainLogicUnit mainLogicUnit = null;

    
    
    public override void Init(LogicUnit logicUnit)
    {
        base.Init(logicUnit);
        mainLogicUnit = logicUnit as MainLogicUnit;

        // 动画移动速度
        aniMoveSpeedBase = mainLogicUnit.moveSpeedBase.RawFloat;

        // 攻击动画速率
        aniAttackSpeedBase = mainLogicUnit.AttackSpeedRate.RawFloat;
        
        // 血条显示
        hpWindow.AddHpItemInfo(mainLogicUnit, hpRoot, mainLogicUnit.Hp.RawInt);
        mainLogicUnit.OnHpChange += OnUpdateHp;
        mainLogicUnit.OnStateChange += OnUpdateState;
        mainLogicUnit.OnSlowDown += OnUpdateJump;
        
        playerWindow = BattleController.Instance.playerWindow;
        playerWindow.AddMiniIconItemInfo(mainLogicUnit);
    }

    protected override void Update()
    {
        if(mainLogicUnit.isDirChanged && !mainLogicUnit.IsSkillSpelling())
        {
            PlayAni(mainLogicUnit.LogicDir.ConvertViewVector3().Equals(Vector3.zero) ? "free" : "walk");
        }
        
        base.Update();
    }

    private void OnDestroy()
    {
        mainLogicUnit.OnHpChange -= OnUpdateHp;
        mainLogicUnit.OnStateChange -= OnUpdateState;
        mainLogicUnit.OnSlowDown -= OnUpdateJump;
    }

    public virtual void OnDeath(MainLogicUnit unit) { }

    public override void PlayAni(string aniName)
    {
        if (aniName == "atk") { aniName = "atk" + Random.Range(1, 3); }
        if (aniName.Contains("walk"))
        {
            var moveRate = mainLogicUnit.LogicMoveSpeed.RawFloat / aniMoveSpeedBase;
            ani[aniName].speed = moveRate;
            ani.CrossFade(aniName, fade / moveRate);
        } 
        else if (aniName.Contains("atk"))
        {
            if (ani.IsPlaying(aniName))
            {
                ani.Stop();
            }

            var attackRate = mainLogicUnit.AttackSpeedRate.RawFloat / aniAttackSpeedBase;
            ani[aniName].speed = attackRate;
            ani.CrossFade(aniName, fade / attackRate);
        }
        else
        {
            if (ani == null)
            {
                Debug.LogError("animation is null");
            }
            ani.CrossFade(aniName, fade);
        }
    }

    private void OnUpdateHp(int hp, JumpUpdateInfo jump)
    {
        if (jump != null)
        {
            var scaleRate = 1.0f * Constants.SCREEN_STANDARD_HEIGHT / Screen.height;
            if (Camera.main != null)
            {
                var screenPos = Camera.main.WorldToScreenPoint(transform.position + new Vector3(0, 1, 0));
                jump.pos = screenPos * scaleRate;
            }
        }

        hpWindow.SetHpValue(mainLogicUnit, hp, jump);
    }

    private void OnUpdateJump(JumpUpdateInfo jump)
    {
        if (jump == null) { return; }
        var scaleRate = 1.0f * Constants.SCREEN_STANDARD_HEIGHT / Screen.height;
        if (Camera.main != null)
        {
            Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position + new Vector3(0, 1, 0));
            jump.pos = screenPos * scaleRate;
        }

        hpWindow.SetJumpUpdateInfo(jump);
    }
    
    private void OnUpdateState(StateEnum state, bool show) {
        if(state is StateEnum.Knockup or StateEnum.Stunned or StateEnum.Silenced) {
            if(mainLogicUnit.IsPlaySelf() && show) {
                playerWindow.SetForbidState();
            }
        }

        hpWindow.SetStateInfo(mainLogicUnit, state, show);
    }
    

    /// <summary>
    /// 设置普攻技能范围
    /// </summary>
    /// <param name="state"></param>
    /// <param name="range"></param>
    public void SetAtkSkillRange(bool state, float range = 2.5f)
    {
        if (skillRange == null) return;
        range += mainLogicUnit.unitData.unitConfig.colliderConfig.mRadius.RawFloat;
        skillRange.localScale = new Vector3(range / 2.5f, range / 2.5f, 1);
        skillRange.gameObject.SetActive(state);
    }

    /// <summary>
    /// 更新技能选择角度
    /// </summary>
    /// <param name="skillRotation"></param>
    public void UpdateSkillRotation(PEVector3 skillRotation)
    {
        viewTargetDir = skillRotation.ConvertViewVector3();
    }
    
    public void SetBuffFollower(BuffView buffView) {
        Transform transform1;
        (transform1 = buffView.transform).SetParent(transform);
        transform1.localPosition = Vector3.zero;
        transform1.localScale = Vector3.one;
    }
    
    public void SetImgInfo(int cdTime) {
        if(mainLogicUnit.IsPlaySelf()) {
            playerWindow.SetImgInfo(cdTime);
        }
    }

    public void RemoveItemInfo()
    {
        hpWindow.RemoveHpItem(mainLogicUnit);
        playerWindow.RemoveMapIconItemInfo(mainLogicUnit);
    }
}

